package org.gap.jseed.utils;

import java.util.Collection;
import java.util.Map;

public class Defense {

	public static void argumentNotNull(Object object) {
		argumentNotNull(object, "Argument cannot be null");
	}

	public static void argumentNotNull(Object object, String message, Object...args) {
		if (object == null) {
			throw new IllegalArgumentException(String.format(message, args));
		}
	}

	public static void valueNotInCollection(Object value, Collection<?> collection) {
		valueNotInCollection(value, collection, "Collection should not contain %s", value);
	}

	public static void valueNotInCollection(Object value, Collection<?> collection, String message, Object...args) {
		if (collection.contains(value)) {
			throw new IllegalArgumentException(String.format(message, args));
		}
	}
	
	public static void valueInCollection(Object value, Collection<?> collection) {
		valueInCollection(value, collection, "Collection should contain %s", value);
	}
	
	public static void valueInCollection(Object value, Collection<?> collection, String message, Object...args) {
		if (!collection.contains(value)) {
			throw new IllegalArgumentException(String.format(message, args));
		}
	}

	public static void keyNotInMap(Object key, Map<?, ?> map) {
		keyNotInMap(key, map, "Map should not contain %s", key);
	}

	public static void keyNotInMap(Object key, Map<?, ?> map, String message, Object...args) {
		valueNotInCollection(key, map.keySet(), message, args);
	}

	public static void keyInMap(Object key, Map<?, ?> map) {
		keyInMap(key, map, "Map should contain %s", key);
	}

	public static void keyInMap(Object key, Map<?, ?> map, String message, Object...args) {
		if (!map.containsKey(key)) {
			throw new IllegalArgumentException(String.format(message, args));
		}
	}
}
